<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2016/12/10
 * Time: 10:28
 */
namespace Admin\Controller;

class BalanceController extends RightController
{

    public function index()
    {
        //判断分页时搜索条件
        if (I("get.p")) {
            $where = session("QG_SEARCH_WHERE_balance");
            $params = session("QG_SEARCH_PARAMS_balance");
            $where = empty($where) ? "1" : $where;
            $pageSize = session("QG_SEARCH_SIZE_balance");
        } else {
            //清除session存储
            session("QG_SEARCH_WHERE_balance", null);
            session("QG_SEARCH_PARAMS_balance", null);
            session("QG_SEARCH_SIZE_balance", null);
            $pageSize = I("post.pageSize");
            $params = I("post.");

            $where = "1";
        }

        // POST数据提交，重新组合检索条件
        if (IS_POST) {
            unset($_GET["p"]);
            $where = $this->searchCondition();
        }

        //$where .= " AND t.isDelete = '2'";
        // 分页输入
        if(empty($pageSize)){
            $pageSize = 10;
        }

        // 总条数
        $count = M('balancelog')
            ->alias("t")
            ->join("left join member m on t.userId=m.id")
            ->where($where)
            ->count();
        $page = new \Admin\Model\Page($count, $pageSize);
        $show = $page->show();

        // 进行分页数据查询
        $list = M('balancelog')
            ->alias("t")
            ->join("left join member m on t.userId=m.id")
            ->field("t.*,m.nickname nickname,m.account account")
            ->where($where)
            ->order('t.id DESC')
            ->limit($page->firstRow . ',' . $page->listRows)->select();

        // 渲染查询条件到session
        session("QG_SEARCH_WHERE_balance", $where);
        session("QG_SEARCH_PARAMS_balance", $params);
        session("QG_SEARCH_SIZE_balance", $pageSize);

        // 输出数据
        $this->assign("params", $params);
        $this->assign('list', $list);
        $this->assign('lists', array(
            "page" => getDropdownList("pageSize" ,"" ,false),
            "type_str" => getDropdownList("type_str" ,"" ,false)
        ));
        $this->assign('page', $show);
        $this->display();
    }

    // 搜索条件
    public function searchCondition()
    {
        $where = "1";

        // 账号
        if (I('post.account')) {
            // 精确
            if (I('post.accountCondition') == 1) {
                $where .= " AND m.account='" . I("post.account") . "'";
            } else {
                $where .= " AND m.account LIKE '%" . I("post.account") . "%'";
            }
        }

        // 名称
        if (I('post.name')) {
            // 精确
            if (I('post.nameCondition') == 1) {
                $where .= " AND t.name='" . I("post.name") . "'";
            } else {
                $where .= " AND t.name LIKE '%" . I("post.name") . "%'";
            }
        }

        // 是否禁用
        if (I('post.isdisable')) {
            $where .= " AND t.isDisable='" . I("post.isdisable") . "'";
        }

        // 创建日期
        if (I('post.createtimeStart') || I('post.createtimeEnd')) {
            // 起始日期
            if(I('post.createtimeStart')){
                $where .= " AND t.createTime>='" . I("post.createtimeStart") . "'";
            }
            // 结束日期
            if(I('post.createtimeEnd')){
                $where .= " AND t.createTime<='" . I("post.createtimeEnd") . " 23:59:59'";
            }
        }

        // 更新日期
        if (I('post.updatetimeStart') || I('post.updatetimeEnd')) {
            // 起始日期
            if(I('post.updatetimeStart')){
                $where .= " AND t.updateTime>='" . I("post.updatetimeStart") . "'";
            }
            // 结束日期
            if(I('post.updatetimeEnd')){
                $where .= " AND t.updateTime<='" . I("post.updatetimeEnd") . " 23:59:59'";
            }
        }

        return $where;
    }

    // 删除
    public function delete()
    {
        if (IS_AJAX) {
            // ID
            $id = intval(I("post.id"));

            // 没有ID
            if(empty($id)){
                $this->ajaxError("错误！");
            }

            // flag
            $data['isDelete'] = "1";

            // 更新时间
            $data['updateTime'] = now_datetime();

            // 更新人
            $data['updateId'] = $this->getAccountId();

            // 更新数据
            $result = M('admin')->where(array("id" => $id))->save($data);

            // 如果成功
            if ($result) {
                $this->ajaxSuccess("禁用成功！");
            } else {
                $this->ajaxError("禁用失败！");
            }
        }
    }
}